package main.java.cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.qo.FormParamets;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IActivityService activityService;


    @RequiredPermission(name="试卷生成页面",expression="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/list";
    }

    @RequiredPermission(name="试卷生成新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/input")
    //把表单提交过来的数据用对象封装
    public String input( Exam exam,  @ModelAttribute("fp") FormParamets fp, Model model) {
        //把表单的数据存到数据库
        examService.save(exam);
        //查询所有的选项(单选,多选,判断)对应的集合,并打乱顺序
        List<Long> single = activityService.getQuestionByType(fp.getSingleChoiceCount(), 1);
        List<Long> multiple = activityService.getQuestionByType(fp.getMultipleChoiceCount(), 2);
        List<Long> judgment = activityService.getQuestionByType(fp.getJudgmentCount(), 3);

        examService.insertRelation(exam.getId(),single,fp.getSingleChoiceCount(),
                                     fp.getSingleChoiceScore());
        examService.insertRelation(exam.getId(),multiple,fp.getMultipleChoiceCount(),
                                    fp.getMultipleChoiceScore());
        examService.insertRelation(exam.getId(),judgment,fp.getJudgmentCount(),
                                    fp.getJudgmentScore());


        List<Exam> singles = examService.allRelation(exam.getId(),1);
        List<Exam> multiples = examService.allRelation(exam.getId(),2);
        List<Exam> judges = examService.allRelation(exam.getId(),3);

        model.addAttribute("singles",singles);
        model.addAttribute("multiples",multiples);
        model.addAttribute("judges",judges);


        return "exam/input";
    }


}
